package cn.b.sky.i18n.service.impl;

import cn.b.sky.i18n.dao.I18nDao;
import cn.b.sky.i18n.enums.LocaleResourceEnum;
import cn.b.sky.i18n.model.EcodeInfo;
import cn.b.sky.i18n.model.LocaleResourceInfo;
import cn.b.sky.i18n.model.SystemLanguageInfo;
import cn.b.sky.i18n.model.SystemTimeZoneInfo;
import cn.b.sky.i18n.service.I18nService;
import cn.b.sky.i18n.utils.LocaleResourceBundle;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service("i18nService")
public class I18nServiceImpl implements I18nService {

  @Resource
  I18nDao i18nDao;

  @Override
  public List<SystemLanguageInfo> listLanguage() {
    return i18nDao.listLanguage();
  }

  @Override
  public List<SystemTimeZoneInfo> listTimeZone() {
    return i18nDao.listTimeZone();
  }

  @Override
  public List<EcodeInfo> listEcode(String languageCode) {
    return i18nDao.listEcode(languageCode);

  }

  @Override
  public List<LocaleResourceInfo> listLocaleResource(String resourceSet, String languageCode) {
    return i18nDao.listLocaleResource(resourceSet, languageCode);
  }

  @Override
  public LocaleResourceInfo getLocaleResourceInfo(String resourceSet, String languageCode, String key) {

    return i18nDao.selectLocaleResourceInfo(resourceSet, languageCode, key);
  }
  @Override
  public List<LocaleResourceInfo> listLocaleResourceBySite(String siteCode, String resourceSet, String languageCode) {
    return i18nDao.listLocaleResourceBySite(siteCode, resourceSet, languageCode);
  }

  @Override
  public void flushLocaleResourceBundle(String siteCode) {
    if (StringUtils.isBlank(siteCode)) {
      return;
    }
    List<SystemLanguageInfo> languages = this.listLanguage();
    //一个LocaleResourceEnum就是一个Bundle
    for (SystemLanguageInfo language : languages) {
      for (LocaleResourceEnum lresource : LocaleResourceEnum.values()) {
        List<LocaleResourceInfo> resourceList = this.listLocaleResourceBySite(siteCode, lresource.getValue(),
            language.getLanguageCode());
        //循环国际化资源，放入每个bundle的resources里
        for (LocaleResourceInfo localeResource : resourceList) {
          //设置setBundle
          LocaleResourceBundle
              .setBundle(localeResource.getSiteCode(), lresource.getValue(), language.getLanguageCode());
          //设置resources
          LocaleResourceBundle
              .getBundle(localeResource.getSiteCode(), lresource.getValue(), language.getLanguageCode()).getResources()
              .put(localeResource.getResourceKey(), localeResource.getResourceValue());
          //设置开启国际化标签
          LocaleResourceBundle
              .getBundle(localeResource.getSiteCode(), lresource.getValue(), language.getLanguageCode());
        }
      }
    }
  }
  
  @Override
  public void flushLocaleResourceBundleByLanguage(String siteCode, String language) {
    if (StringUtils.isBlank(language) 
        || StringUtils.isBlank(siteCode)) {
      return;
    }
    //一个LocaleResourceEnum就是一个Bundle 
    for (LocaleResourceEnum lresource : LocaleResourceEnum.values()) {
      List<LocaleResourceInfo> resourceList = this.listLocaleResourceBySite(siteCode, lresource.getValue(),
          language);
      //循环国际化资源，放入每个bundle的resources里
      for (LocaleResourceInfo localeResource : resourceList) {
        //设置setBundle
        LocaleResourceBundle
            .setBundle(localeResource.getSiteCode(), lresource.getValue(), language);
        //设置resources
        LocaleResourceBundle
            .getBundle(localeResource.getSiteCode(), lresource.getValue(), language).getResources()
            .put(localeResource.getResourceKey(), localeResource.getResourceValue());
        //设置开启国际化标签
        LocaleResourceBundle
            .getBundle(localeResource.getSiteCode(), lresource.getValue(), language);
      }
    }
  }

  @Override
  public void deleteLocaleResourceBundle(String siteCode, String language) {
    if(!StringUtils.isBlank(siteCode) 
        && !StringUtils.isBlank(language)){
      for (LocaleResourceEnum lresource : LocaleResourceEnum.values()) {
        LocaleResourceBundle.clearBundle(siteCode, lresource.getValue(), language);
      }
    }
  }
}